package com.vvkee.office.service;

import com.vvkee.office.utils.FileConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;

@Service
@Slf4j
public class FileService {

	/**
	 * @Description:系统文件在线预览接口
	 * @Author: tarzan
	 */
	public void onlinePreview(String url, HttpServletResponse response) throws Exception {
		log.info("url is {}", url);
		//获取文件类型
		String[] str = StringUtils.split(url, "\\.");
		log.info("str is {}", str);

		if (str.length == 0) {
			throw new Exception("文件格式不正确");
		}
		String suffix = str[str.length - 1];
		log.info("suffix is {}", suffix);
		if (!suffix.equals("txt") && !suffix.equals("doc") && !suffix.equals("docx") && !suffix.equals("xls")
				&& !suffix.equals("xlsx") && !suffix.equals("ppt") && !suffix.equals("pptx")) {
			throw new Exception("文件格式不支持预览");
		}
		InputStream in = FileConvertUtil.convertLocaleFile(url, suffix);
		OutputStream outputStream = response.getOutputStream();
		//创建存放文件内容的数组
		byte[] buff = new byte[1024];
		//所读取的内容使用n来接收
		int n;
		//当没有读取完时,继续读取,循环
		while ((n = in.read(buff)) != -1) {
			//将字节数组的数据全部写入到输出流中
			outputStream.write(buff, 0, n);
		}
		//强制将缓存区的数据进行输出
		outputStream.flush();
		//关流
		outputStream.close();
		in.close();
	}
}
